
var data = [];

$(function() {

	$("input[id^='code_s']:visible").autocomplete(Test.quote_code);
	
	$("input[id^='customer_s']:visible").autocomplete(Test.customer);
	
	$("input[id^='bj_s']:visible").autocomplete(Test.emp);
	
	_ck_default("input[id^='applicant_s']:visible", "input[id^='applicant_t']:visible");
	
	Tip.emp('业务员');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("业务员：");
		} else if (a == 'oper') {
			label.html("录入员：");
		}
	});
});

var QuoteMy = {
	
	project_name: function (s) {
		if (s == null || s.trim() == '') {
			return true;
		}
	
		var value = s.trim();
		var regex;
	
		regex = /^(GM|TM|T)(\d+)\.(\d+)$/
		if (regex.test(value)) {
			return true;
		}
	
		return false;
	},
	
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_add")[0];
				
				if (!this.project_name(form.code.value)) {
					alert("项目编号不符合规范，请重新命名！");
					return false;
				}
	
				var body = { };
				body.id = form.id.value;
				body.code = form.code.value;
				body.num = form.num.value;
				
				body.category = form.category.value;
				body.urgent = form.urgent.value;
				body.date = form.date.value;
				body.content = form.content.value;
	
				$(this).dialog("close");
	
				var url = 'customer.php?do=QuoteMy.addPost';
	
				$.post(url, body, function(data) {
					var json = $.evalJSON(data);
					
					if (json.status == 1) {
						alert("添加成功！");
					} else {
						alert("项目编号已存在，添加失败，请重新添加！");
					}				
				});
			}
		});
	
		dialog_add(url, title, xy);
	},

	/**
	 * 批量更新
	 */
	dialog_uu: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=QuoteMy.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"更新": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};					
						body.field = form.field.value;
						body.data1 = form.data1.value;
						body.data2 = form.data2.value;

						$(this).dialog("close");

						var url = 'customer.php?do=QuoteMy.uuPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'customer.php?do=QuoteMy.edit&id=' + id + p2;
		var c2 = 'customer.php?do=QuoteMy.view&id=' + id + p2;
		
		var c3 = 'customer.php?do=QuoteInfo.findPost&quote_s=' + id;	
		var c4 = 'mold.php?do=MoldMy.add&id=' + id;
			
		var a1 = ['编辑信息', 'url', 'QuoteMy.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];

		var a3 = ['相关信息', 'url', 'list'];	
		var a4 = ['产品下单', 'url', 'QuoteMy.dialog_mold', '650x400'];
		
		if (status < 3) {
			var s = c1 + ", " + c2 + ", " + c3 + ", " + c4;

			return this.popup_(e, [a1, a2, a3, a4], s);
		}

		if (status < 8) {
			var s = c2 + ", " + c3;

			return this.popup_(e, [a2, a3], s);
		}

		// 完成报价
		if (status == 8 || status ==9) {
			var d1 = 'customer.php?do=QuoteInfo.add&id=' + id + "&requote=1";
			var b1 = ['重新报价', 'url', 'QuoteMy.dialog_requirement', '650x400'];
					
			var s = c4 + ", " + c2 + ", " + c3 + ", " + d1;

			return this.popup_(e, [a4, a2, a3, b1], s);
		}

		var s = c4 + ", " + c2 + ", " + c3;

		return this.popup_(e, [a4, a2, a3], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c5 = 'customer.php?do=QuoteMy.item&id=' + id;
		var a5 = ['报价明细', 'url', 'QuoteMy.dialog_item', '780x450'];
		
		var ss = 'customer.php?do=QuoteMy.ss&id=' + id;
		var bb = ['修改状态', 'url', 'QuoteMy.dialog_ss', '400x200'];
		
		s += ", " + c5 + ", " + ss;
		a.push(a5);
		a.push(bb);
		
		return popup(e, a, s);
	},
	
	dialog_item: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
				
				var body = { };
				body.id = form.id.value;
				body.code = form.code.value;
				body.num = form.num.value;
				
				body.category = form.category.value;
				body.urgent = form.urgent.value;
				body.date = form.date.value;
				
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				
				body.content = form.content.value;
				
				// body.item = data;
				body.item = $.toJSON(data);
				
				$(this).dialog("close");
	
				var url = 'customer.php?do=QuoteMy.itemPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=QuoteMy.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'customer.php?do=QuoteMy.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=QuoteMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.code = form.code.value;
						body.num = form.num.value;
						
						body.category = form.category.value;
						body.urgent = form.urgent.value;
						body.date = form.date.value;
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'customer.php?do=QuoteMy.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_mold: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				if (!QuoteMy.mold_code(form.code.value, form.num.value)) {
					alert("产品编号不符合命名规范，请重新输入！");
					return false;
				}
				
				var body = { };
				body.id = form.id.value;
				body.customer = form.customer.value;
				body.date = form.date.value;
				
				body.code = form.code.value;
				body.category = form.category.value;
				
				body.emp = form.emp.value;
				body.content = form.content.value;

				$(this).dialog("close");

				var url = 'mold.php?do=MoldMy.addPost';

				$.post(url, body, function(data) {
					alert("添加成功！");

					Quote.list_();
				});
			}
		});

		dialog_add(url, title, xy);
	},

	mold_code: function (s, num) {
		if (s == null || s.trim() == '') {
			return true;
		}
		
		var value = s.trim();
		var regex;

		if (num != null && num > 1) {
			regex = /^(GM|TM|T)(\d+)\.(\d+)-(\d+)$/;
		} else {
			regex = /^(GM|TM|T)(\d+)\.(\d+)$/;
		}
		
		if (regex.test(value)) {
			return true;
		}

		return false;
	},

	a2: function (e, p2, id, status) {	
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'customer.php?do=QuoteMy.status&id=' + id + p2;

		var c2 = 'customer.php?do=QuoteMy.reason&id=' + id + p2;
		var c3 = 'customer.php?do=QuoteMy.bj&id=' + id + p2;
		
		var c4 = 'customer.php?do=QuoteInfo.add&id=' + id;
		
		var a3 = ['指定报价', 'url', 'QuoteMy.dialog_bj', '600x360'];	

		var a5 = ['暂停报价', 'url', 'QuoteMy.dialog_stop', '600x400'];
		var a6 = ['取消报价', 'url', 'QuoteMy.dialog_cancel', '600x400'];	
			
		var a4 = ['报价需求', 'url', 'QuoteMy.dialog_requirement', '650x430'];

		// 报价申请
		if (status == 1) {		
			var s = c4;

			return popup(e, [a4], s);
		}
		
		// 等待报价
		if (status == 2) {
			var b1 = ['开始报价', 'url', 'QuoteMy.list_start'];
			
			var s = c1 + "&status=3, " + c4 + ", " + c2 + ", " + c2 + ", " + c3;

			return popup(e, [b1, a4, a5, a6, a3], s);
		}

		// 正在报价
		if (status == 3) {
			var b1 = ['完成报价', 'url', 'QuoteMy.dialog_finish', '650x430', '添加报价信息'];
			
			var s = c4 + "&status=8, " + c2 + ", " + c2;

			return popup(e, [b1, a5, a6], s);
		}
		
		// 暂停报价
		if (status == 5) {
			var d1 = 'customer.php?do=QuoteMy.status_continue&id=' + id + p2;
			var b1 = ['继续报价', 'url', 'QuoteMy.list_continue'];
			
			var s = d1 + ", " + c2 + ", " + c3;

			return popup(e, [b1, a6, a3], s);
		}
		
		// 取消报价
		if (status == 6) {
			var d1 = 'customer.php?do=QuoteMy.status_restart&id=' + id + p2;
			var b1 = ['重启报价', 'url', 'QuoteMy.list_restart'];
			
			var s = d1 + ", " + c3;

			return popup(e, [b1, a3], s);
		}

		var s = c3;

		return popup(e, [a3], s);
	},

	list_restart: function (url, view) {
		$.getJSON('customer.php?do=QuoteMy.ajax_reason', function(json) {
			if (json.status == 1) {
				if (confirm("是否要重启报价？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	list_start: function (url, view) {
		$.getJSON('customer.php?do=QuoteMy.ajax_bj', function(json) {
			if (json.status == 1) {
				if (confirm("是否要开始报价？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	dialog_finish: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=QuoteMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = { };
						body.id = form.id.value;
						body.num = form.num.value;
						body.type = 2;
						body.content = form.content.value;

						var url = 'customer.php?do=QuoteInfo.addPost';

						$.post(url, body, function(data) {
							var a = $.evalJSON(data);
							$('#info').val(a.id);
							
							$('#userfile').uploadify('upload');

							Quote.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_continue: function (url, view) {
		$.getJSON('customer.php?do=QuoteMy.ajax_reason', function(json) {
			if (json.status == 1) {
				if (confirm("是否继续报价？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	/**
	 * 暂停
	 */
	dialog_stop: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=QuoteMy.ajax_reason', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否暂停？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 5;

						$(this).dialog("close");

						var url = 'customer.php?do=QuoteMy.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=QuoteMy.ajax_reason', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否取消？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 6;

						$(this).dialog("close");

						var url = 'customer.php?do=QuoteMy.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 指定报价
	 */
	dialog_bj: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=QuoteMy.ajax_b', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"指定": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.bj = form.bj.value;

						$(this).dialog("close");

						var url = 'customer.php?do=QuoteMy.bjPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_requirement: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=QuoteMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = { };
						body.id = form.id.value;
						body.requote = form.requote.value;
						body.type = 1;
						body.content = form.content.value;

						var url = 'customer.php?do=QuoteInfo.addPost';

						$.post(url, body, function(data) {
							var a = $.evalJSON(data);
							$('#info').val(a.id);
							
							$('#userfile').uploadify('upload');

							Quote.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_: function () {
		var result = Common.result();
		var url = 'customer.php?do=QuoteMy.findPost';

		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.get(url, function(data) {
			$(result).html(data);
		});
	}
};


function TypeSelectCellEditor(args) {
	var $select;
    var defaultValue;
    var scope = this;

    this.init = function() {
        $select = $("<SELECT tabIndex='0' id='type_'></SELECT>");
        
        $select.empty();
        // alert(currency_json);
        $.each(type_json, function(k, v) {
        	// alert(k + " = " + v);
        	var item = new Option(v, k);
        	$select[0].options.add(item);
        });
        
        $select.appendTo(args.container);        
        $select.focus();
    };

    this.destroy = function() {
        $select.remove();
    };

    this.focus = function() {
        $select.focus();
    };

    this.loadValue = function(item) {
    	$select.val(item[args.column.field]);
    	$select.select();
    };

    this.serializeValue = function() {
        return $select.val();
    };

    this.applyValue = function(item,state) {    	
        // item[args.column.field] = state;
        
    	var k = args.column.field;
        // item[k] = state;
    	var s = $select.find("option:selected").text();
    	item[k] = state + ":" + s;
    };
   
    this.isValueChanged = function() {
        return ($select.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}
